<?php

namespace app\controller\admin\cnf;

use app\bll\OrganPost as BllOrganPost;
use core\controller\AdminBase;
use Exception;
use support\View;

class  OrganPost extends AdminBase
{
    public  function index()
    {
        $bllOrganPost =  new BllOrganPost();
        $posts = $bllOrganPost->paginator(title: request()->get('title', ''));
        View::assign('posts', $posts);
        return $this->view();
    }

    public function add()
    {
        if (is_post()) {
            try {
                $bllOrganPost =  new BllOrganPost();
                $bllOrganPost->add(request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view();
        }
    }

    public function edit()
    {
        $id = request()->get('id');
        $bllOrganPost =  new BllOrganPost();
        if (is_post()) {
            try {

                $bllOrganPost->edit($id, request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $info = $bllOrganPost->find($id);
            View::assign('info', $info);
            return $this->view();
        }
    }

    public function delete()
    {
        try {
            $id = request()->get('id');
            $bllOrganPost =  new BllOrganPost();
            $bllOrganPost->delete($id);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
